package dao.imp;

import dao.CourseContentDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.Course;
import pojo.Course_Lesson;
import pojo.Course_Section;
import utils.DateUtils;
import utils.DruidUtils;

import java.sql.SQLException;
import java.util.List;

public class CourseContentDaoImpl implements CourseContentDao {
    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {
        try {
            QueryRunner queryRunner =new QueryRunner(DruidUtils.getDataSource());

            String sql = "SELECT id,course_id,section_name,description,order_num,status FROM course_section WHERE course_id = ? ;";

            List<Course_Section> sectionlist = queryRunner.query(sql, new BeanListHandler<Course_Section>(Course_Section.class), courseId);

            for (Course_Section course_section : sectionlist) {
                List<Course_Lesson> lessonsList = findLessonBySectionId(course_section.getId());
                course_section.setLessonList(lessonsList);
            }
            return sectionlist;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Course_Lesson> findLessonBySectionId(int sectionId) {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql = "SELECT id,course_id,section_id,theme,duration,is_free,order_num,status FROM course_lesson WHERE section_id = ?;";

            List<Course_Lesson> lessonList = queryRunner.query(sql, new BeanListHandler<Course_Lesson>(Course_Lesson.class), sectionId);
            return lessonList;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Course findCourseByCourseId(int courseId) {
        try {
            QueryRunner queryRunner =new QueryRunner(DruidUtils.getDataSource());

            String sql = "select id,course_name from course where id = ?";

            Course course = queryRunner.query(sql, new BeanHandler<Course>(Course.class), courseId);

            return course;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public int saveSection(Course_Section course_section) {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql = "INSERT INTO course_section(course_id,section_name,description,order_num,STATUS,create_time,update_time) VALUES(?,?,?,?,?,?,?);";

            Object[] param = {course_section.getCourse_id(),course_section.getSection_name(),course_section.getDescription(),course_section.getOrder_num(),course_section.getStatus(),course_section.getCreate_time(),course_section.getUpdate_time()};

            int row = queryRunner.update(sql, param);

            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateSection(Course_Section section) {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql = "UPDATE course_section SET section_name = ?,description = ?,order_num = ?,update_time = ?  WHERE id = ?;";

            Object[] param = {section.getSection_name(),section.getDescription(),section.getOrder_num(),section.getUpdate_time(),section.getId()};

            int row = queryRunner.update(sql, param);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateSectionStatus(int id, int status) {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql  = "update course_section set status = ?,update_time = ? where id = ?;";

            Object[] param = {status, DateUtils.getDateFormart(),id};

            int update = queryRunner.update(sql, param);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Course_Section findSectionBySectionId(int sectionId) {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql = "select id,section_name from course_section where id = ?;";

            Course_Section section = queryRunner.query(sql, new BeanHandler<Course_Section>(Course_Section.class), sectionId);
            return section;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int saveLesson(Course_Lesson lesson) {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql = "insert into course_lesson(course_id,section_id,theme,duration,is_free,order_num,status,create_time,update_time) values(?,?,?,?,?,?,?,?,?);";

            Object[] param = {lesson.getCourse_id(),lesson.getSection_id(),lesson.getTheme(),lesson.getDuration(),lesson.getIs_free(),lesson.getOrderNum(),lesson.getStatus(),lesson.getCreate_time(),lesson.getUpdate_time()};


            int update = queryRunner.update(sql, param);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public int updateLesson(Course_Lesson lesson) {
        try {
            QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

            String sql = "update course_lesson set theme=?,duration=?,is_free=?,order_num=?,update_time=? where id= ?;";

            Object[] param = {lesson.getTheme(),lesson.getDuration(),lesson.getIs_free(),lesson.getOrderNum(),lesson.getUpdate_time(),lesson.getId()};

            int update = queryRunner.update(sql, param);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }

    }
}
